package com.zj.study;

public class 最长回文子串_中心扩展 {

    public static void main(String[] args) {
        System.out.println(longestPalindrome("cbbdbbabbd"));
    }


    public static String longestPalindrome(String s) {
        int len = 1;
        int start = 0;
        for (int i = 0; i < s.length(); i++) {
            int len1 = getExpandLen(s, i, i);
            int len2 = getExpandLen(s, i, i + 1);
            int maxLen = Math.max(len1, len2);
            if (maxLen > len) {
                len = maxLen;
                start = i - (len - 1) / 2;
            }
        }
        return s.substring(start, start + len);
    }

    public static int getExpandLen(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        return right - left - 1;
    }


}
